﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.PQ
{
    public partial class ht_comp_add : PageBase
    {
     
        public string whe = "", path = "", tab = "pq_ht";
        public string us, um, ubh; 

        protected void Page_Load(object sender, EventArgs e)
        {

            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();

            if (!IsPostBack)
            {
                //if (!String.IsNullOrEmpty(Request["stype"]))
                //{
                //     hidstype.Text = Request["stype"].Trim();

                //}
                fill();
                Regcomp();
            }
            else
            {
                fillcomp();                
            }
                    
        }

      void Regcomp()
      {
          ExtAspNet.PageContext.RegisterStartupScript
                         (
                         "var d=Ext.getCmp('" + ddlcompany.ClientID + "');" +
                         " var oldInput=\"\";" +
                         " d.listWidth=500; " +
                         " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                         " var newInput=document.getElementById(\"" + ddlcompany.ClientID + "\").value;" +
                          " if (oldInput==newInput) {return;} " +
                         " oldInput=newInput;" +
                          " __doPostBack('loadcomp',newInput);" +
                          "});"
                         );

          ExtAspNet.PageContext.RegisterStartupScript
               (
               "var d=Ext.getCmp('" + ddlperson.ClientID + "');" +
               " var oldInput=\"\";" +
               " d.listWidth=500; " +
               " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
               " var newInput=document.getElementById(\"" + ddlperson.ClientID + "\").value;" +
                " if (oldInput==newInput) {return;} " +
               " oldInput=newInput;" +
                " __doPostBack('loadpepo',newInput);" +
                "});"
               );



     ExtAspNet.PageContext.RegisterStartupScript
       (
       "var d=Ext.getCmp('" + ddlzsales.ClientID + "');" +
       " var oldInput=\"\";" +
       " d.listWidth=500; " +
       " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
       " var newInput=document.getElementById(\"" + ddlzsales.ClientID + "\").value;" +
        " if (oldInput==newInput) {return;} " +
       " oldInput=newInput;" +
        " __doPostBack('loadzss',newInput);" +
        "});"
       );

      }

      void fillcomp()
      {

          if (Request["__EVENTTARGET"] == "loadcomp")
          {

              string sqltxt = "SELECT * FROM company where charindex('" +
                  Request["__EVENTARGUMENT"].ToString() + "',cname)>0 ";
              // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

              DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
              ddlcompany.Items.Clear();
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  ddlcompany.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["cname"].ToString(), dt.Rows[i]["cname"].ToString()));
              }


              //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
              ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlcompany.ClientID + "\").value='" +
                       Request["__EVENTARGUMENT"] + "';");

          }


          if (Request["__EVENTTARGET"] == "loadpepo")
          {

              string sqltxt = "SELECT * FROM sys_user where charindex('" +
                  Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
              // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

              DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
              ddlperson.Items.Clear();
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  ddlperson.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
              }


              //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
              ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlperson.ClientID + "\").value='" +
                       Request["__EVENTARGUMENT"] + "';");

          }



          if (Request["__EVENTTARGET"] == "loadzss")
          {
              string sqltxt = "SELECT * FROM sys_user where charindex('" +
                  Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
              // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

              DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
              ddlzsales.Items.Clear();
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  ddlzsales.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
              }


              //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
              ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlzsales.ClientID + "\").value='" +
                       Request["__EVENTARGUMENT"] + "';");

          }

      }



        //填 充ddl
        void fill()
        { 
            DataTable e; 
       
            //驻厂员工
            e = BLL.Bllbase.get_table("[sys_user]");
            ddlzsales.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzsales.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["user_name"].ToString().Trim()));
            }
            ddlzsales.DataBind();
            ddlzsales.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlzsales.SelectedIndex = ddlzsales.Items.IndexOf(ddlzsales.Items.FindByText(""));
            

            //公司
            e = BLL.Bllbase.get_table("[company]");
            ddlcompany.Items.Clear();
         
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcompany.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));

            }
            ddlcompany.DataBind();           

            ddlcompany.Items.Add(new ExtAspNet.ListItem("", ""));           
            ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByText(""));
           


            //员工
            e = BLL.Bllbase.get_table("[sys_user]");
            ddlperson.Items.Clear();           

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlperson.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
            }
            ddlperson.DataBind();          
            ddlperson.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByText(""));
         

         
        }
      


        protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = ddltype.SelectedValue;
             //公司
            DataTable se = BLL.Bllbase.get_table(type);

            ddlcompany.Items.Clear();

            if (type == "company")
            {
                for (int i = 0; i < se.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(se.Rows[i]["cname"].ToString().Trim(), se.Rows[i]["cbh"].ToString().Trim()));
                }
            }

            if (type == "school")
            {
                for (int i = 0; i < se.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(se.Rows[i]["schname"].ToString().Trim(), se.Rows[i]["schbh"].ToString().Trim()));
                }
            }

            if (type == "zj")
            {
                for (int i = 0; i < se.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(se.Rows[i]["zname"].ToString().Trim(), se.Rows[i]["zbh"].ToString().Trim()));
                }
            }
            ddlcompany.DataBind();
        }

  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {  
           
             add_member();
            
            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(hidstype.Text) + ActiveWindow.GetHidePostBackReference());
        }

        void add_member()
        {
           Model.PQ.comp_ht s = new Model.PQ.comp_ht();

           s.htbh = txtht.Text;

           s.pbh = ddlperson.SelectedValue;
           s.pname = ddlperson.SelectedText;
           s.pvendor = ddlcompany.SelectedText;

           s.pvbh= ddlcompany.SelectedValue;

           s.httype = ddltype.SelectedValue;
            
           s.snum = int.Parse(txtnum.Text);
                    
           if (VAN.IsDateTime(date1.Text))
              s.sdate = DateTime.Parse(date1.Text);
           else
              s.sdate = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(date2.Text))
                s.edate = DateTime.Parse(date2.Text);
            else
                s.edate = DateTime.Parse("1900-01-01");
            
            if (VAN.IsDateTime(regedate.Text))
                s.rdage = DateTime.Parse(regedate.Text);
            else
                s.rdage = DateTime.Parse("1900-01-01");

            s.ftype = "";

            s.fbh = "";

            s.fname = "";

            s.owen = "";
           
            s.ftype =linknm.Text; //联系人
            s.fbh = linktel.Text; //联系电话

            //ddlcomp.SelectedText;          
            s.fname = "";  
         
            //s.owen = ddlsales.SelectedText;          

            s.zsales = ddlzsales.SelectedValue;          

            s.money = 0;
            s.money =decimal.Parse(txtmoney.Text);

            s.work = "";
            s.work  =txtworktype.Text;

            s.status = 0;

            s.memo = "";
            s.memo = txtmemo.Text;

            BLL.PQ.comp_ht t = new BLL.PQ.comp_ht();
            t.Add(s);

            //更新业务员操作记录
            // BLL.Bllbase.update_rec(tab, um, " htbh='" + s.htbh + "' ");

            string sqltxt = "update  comp_ht set  updtime=getdate(), sales='" + um + "',sbh='" + ubh + "' where 1=1 and htbh='" + s.htbh + "'";
            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            BLL.BLLfun.up_sales_num_add(s.httype, s.pvbh); //添加到企业签约次数
        }
       
    }
}